package com.ruoyi.business.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.business.domain.CommonReviewInfo;
import com.ruoyi.business.domain.WxCarDevice;
import com.ruoyi.business.domain.WxUserFace;
import com.ruoyi.business.domain.vo.CommonReviewVO;
import com.ruoyi.business.runner.InitRabbitMQRunner;
import com.ruoyi.business.service.ICommonReviewInfoService;
import com.ruoyi.business.service.IWxCarDeviceService;
import com.ruoyi.business.vo.WxDeviceConstants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WxCarMapper;
import com.ruoyi.business.domain.WxCar;
import com.ruoyi.business.service.IWxCarService;

import static com.ruoyi.business.weapp.CommonContants.REVIEW_TYPE_CAR;
import static com.ruoyi.business.weapp.CommonContants.REVIEW_TYPE_REPAIR;

/**
 * 车辆认证管理Service业务层处理
 * 
 * @author asher
 * @date 2021-11-19
 */
@Service
public class WxCarServiceImpl implements IWxCarService 
{
    @Autowired
    private WxCarMapper wxCarMapper;

    @Autowired
    private ICommonReviewInfoService commonReviewInfoService;

    @Autowired
    private IWxCarDeviceService wxCarDeviceService;

    @Autowired
    private InitRabbitMQRunner initRabbitMQRunner;

    /**
     * 查询车辆认证管理
     * 
     * @param carId 车辆认证管理主键
     * @return 车辆认证管理
     */
    @Override
    public WxCar selectWxCarByCarId(Long carId)
    {
        return wxCarMapper.selectWxCarByCarId(carId);
    }

    /**
     * 查询车辆认证管理列表
     * 
     * @param wxCar 车辆认证管理
     * @return 车辆认证管理
     */
    @Override
    public List<WxCar> selectWxCarList(WxCar wxCar)
    {
        return wxCarMapper.selectWxCarList(wxCar);
    }

    /**
     * 新增车辆认证管理
     * 
     * @param wxCar 车辆认证管理
     * @return 结果
     */
    @Override
    public int insertWxCar(WxCar wxCar)
    {
        wxCar.setCreateTime(DateUtils.getNowDate());
        return wxCarMapper.insertWxCar(wxCar);
    }

    /**
     * 修改车辆认证管理
     * 
     * @param wxCar 车辆认证管理
     * @return 结果
     */
    @Override
    public int updateWxCar(WxCar wxCar)
    {
        wxCar.setUpdateTime(DateUtils.getNowDate());
        return wxCarMapper.updateWxCar(wxCar);
    }

    /**
     * 批量删除车辆认证管理
     * 
     * @param carIds 需要删除的车辆认证管理主键
     * @return 结果
     */
    @Override
    public int deleteWxCarByCarIds(Long[] carIds) {
        try {
            for (Long carId : carIds) {
                WxCar wxCar = wxCarMapper.selectWxCarByCarId(carId);
                this.deleteCarNumber(wxCar);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("设备删除失败");
        }
        return wxCarMapper.deleteWxCarByCarIds(carIds);
    }

    /**
     * 删除车辆认证管理信息
     * 
     * @param carId 车辆认证管理主键
     * @return 结果
     */
    @Override
    public int deleteWxCarByCarId(Long carId)
    {
        return wxCarMapper.deleteWxCarByCarId(carId);
    }

    @Override
    public int reviewCar(CommonReviewVO commonReviewVO) throws IOException {
        WxCar wxCar = wxCarMapper.selectWxCarByCarId(commonReviewVO.getRecordId());
        if (wxCar == null) {
            return 0;
        }
        WxCarDevice wxCarDevice = new WxCarDevice();
        wxCarDevice.setVillageId(wxCar.getVillageId());
        List<WxCarDevice> wxCarDevices = wxCarDeviceService.selectWxCarDeviceList(wxCarDevice);
        if (wxCarDevices.isEmpty()) {
            return 0;
        }

        CommonReviewInfo commonReviewInfo = new CommonReviewInfo();
        commonReviewInfo.setOriginalStatus(wxCar.getStatus());
        commonReviewInfo.setReviewContent(commonReviewVO.getContent());
        commonReviewInfo.setDestStatus(commonReviewVO.getStatus());
        commonReviewInfo.setCreateBy(commonReviewVO.getUserId().toString());
        commonReviewInfo.setBusinessId(commonReviewVO.getRecordId());
        commonReviewInfo.setReviewType(REVIEW_TYPE_CAR);
        wxCar.setStatus(commonReviewVO.getStatus());

        if (commonReviewVO.getStatus().equals("0")) {
            return commonReviewInfoService.insertCommonReviewInfo(commonReviewInfo) & wxCarMapper.updateWxCar(wxCar);
        }
        if ("2".equals(commonReviewVO.getStatus())) {
            dispatchCarNumber(wxCar);
            wxCar.setRemark("1");
            wxCar.setUpdateTime(new Date());
        }
        return commonReviewInfoService.insertCommonReviewInfo(commonReviewInfo) & wxCarMapper.updateWxCar(wxCar);
    }

    public String cmdJsonStr(String cmd, String carNumber, String wxUserId, String carId) throws UnsupportedEncodingException {
        String cmdJsonStr = new String("{ \"cmd\": \"white_list_operator\", \"id\":\"" + carId + "\", \"operator_type\": \"" + cmd +
                "\", \"dldb_rec\": { \"create_time\": \"" + DateUtils.getTime() + "\", \"enable_time\": \"" +
                DateUtils.getTime() + "\", \"overdue_time\": \"2040-10-20 12:30:40\", \"enable\": 1, \"plate\": \"" + carNumber
                + "\", \"time_seg_enable\": 0, \"seg_time\": \"2016-10-20 12:30:40\", \"need_alarm\": 0, \"vehicle_code\": \"" +
                carId + "business\", \"vehicle_comment\": \"下发\", \"customer_id\": " + wxUserId +" } }");
        return cmdJsonStr;
    }

    public boolean dispatchCarNumber(WxCar wxCar) throws IOException {
        WxCarDevice wxCarDevice = new WxCarDevice();
        wxCarDevice.setVillageId(wxCar.getVillageId());
        List<WxCarDevice> wxCarDevices = wxCarDeviceService.selectWxCarDeviceList(wxCarDevice);
        for (WxCarDevice item : wxCarDevices) {
            if (WxDeviceConstants.ZHEN_SHI_DEVICE.equals(item.getDeviceType())) {
                initRabbitMQRunner.sendMessage(item,cmdJsonStr("update_or_add", wxCar.getNumber(), wxCar.getUserId().toString(), wxCar.getCarId().toString()));
            } else if (WxDeviceConstants.QIAN_XI_DEVICE.equals(item.getDeviceType())) {
                initRabbitMQRunner.sendMessage(item,cmdJsonStr("update_or_add", wxCar.getNumber(), wxCar.getUserId().toString(), wxCar.getCarId().toString()));
            }

        }
        return true;
    }

    public String cmdDeleteStr(String cmd, String carNumber, String wxUserId, String carId) throws UnsupportedEncodingException {
        String cmdDeleteStr = "{ \"cmd\":\"white_list_operator\", \"id\" : \"" + carId + "\", \"operator_type\":\"delete\", \"plate\":\"" + carNumber + "\" }";
        return cmdDeleteStr;
    }

    public boolean deleteCarNumber(WxCar wxCar) throws IOException {
        WxCarDevice wxCarDevice = new WxCarDevice();
        wxCarDevice.setVillageId(wxCar.getVillageId());
        List<WxCarDevice> wxCarDevices = wxCarDeviceService.selectWxCarDeviceList(wxCarDevice);
        for (WxCarDevice item : wxCarDevices) {
            if (WxDeviceConstants.ZHEN_SHI_DEVICE.equals(item.getDeviceType())) {
                initRabbitMQRunner.sendMessage(item,cmdDeleteStr("delete", wxCar.getNumber(), wxCar.getUserId().toString(), wxCar.getCarId().toString()));
            } else if (WxDeviceConstants.QIAN_XI_DEVICE.equals(item.getDeviceType())) {
                initRabbitMQRunner.sendMessage(item,cmdDeleteStr("delete", wxCar.getNumber(), wxCar.getUserId().toString(), wxCar.getCarId().toString()));
            }

        }
        return true;
    }

    public String generateId() {
        return String.valueOf(new Random().nextInt(1000));
    }

}
